<script>
import request from '@/api/http.js';
export default {
	onLaunch: function () {
		console.log('App Launch');
		if (!uni.getStorageSync('token')) {
			console.log('没有token，尝试自动登录获取');
			this.autoLogin();
		} else {
			this.verifyLogin();
		}
	},
	onShow: function () {
		console.log('App Show');
	},
	onHide: function () {
		console.log('App Hide');
	},
	methods: {
		autoLogin() {
			uni.login({
				provider: 'weixin',
				async success(res) {
					if (res.code) {
						console.log('获取临时code成功');
						const resp = await request({
							url: '/wx/login?code=' + res.code,
							method: 'post'
						});
						console.log('--登录成功获得--', resp);
						uni.showToast({
							title: '登录成功'
						});
						if (resp.token) {
							uni.setStorageSync('token', resp.token);
						} else {
							uni.showToast({
								title: 'token未获取'
							});
							console.log('--登录获得token失败--', resp);
						}
					}
				}
			});
		},
		async verifyLogin() {
			try {
				const res = await request({
					url: '/wx/currentUID',
					method: 'GET'
				});
				console.log('校验成功', res);
			} catch (e) {
				this.autoLogin()
			}
		}
	}
};
</script>

<style>
/*每个页面公共css */
</style>
